<?php

namespace App\Services\Gallery\Services;

use App\Services\Gallery\Interfaces\ClassicCaseServiceInterface;
use App\Services\Gallery\Model\ClassicCase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ClassicCaseService implements ClassicCaseServiceInterface
{
    public function index($request)
    {
        $keywords = $request['keywords'] ?? '';
        $is_show = $request['is_show'] ?? '';
        $sort = $request['sort'] ?? 'sort';
        $order = $request['order'] ?? 'asc';
        $size = $request['size'] ?? 10;
        $type = $request['type'] ?? '';
        $is_recommend = $request['is_recommend'] ?? '';
        $data = ClassicCase::query()
            ->when($keywords, function ($query) use ($keywords) {
                return $query->where('title', 'like', '%' . $keywords . '%');
            })
            ->when($is_show, function ($query) use ($is_show) {
                return $query->where('is_show', $is_show);
            })
            ->when($is_recommend != '', function ($query) use ($is_recommend) {
                return $query->where('is_recommend', '=', $is_recommend);
            })
            ->when($type, function ($query) use ($type) {
                return $query->where('type', $type);
            })
            ->orderBy("is_show", "desc")
            ->orderBy($sort, $order)
            ->orderBy("updated_at", "desc")
            ->paginate($size);

        $data->appends($request);

        return $data->toArray();
    }

    public function set($request, $id = 0)
    {
        if ($id) {
            $data = ClassicCase::findOrFail($id);
        } else {
            $data = new ClassicCase();
        }
        $data->user()->associate(Auth::id());
        $data->fill($request)->save();

        return $data->toArray();
    }

    public function get($id)
    {
        return ClassicCase::query()->where('id', $id)->firstOrFail()->toArray();
    }

    public function destroy($id)
    {
        $data = ClassicCase::findOrFail($id);
        $data->delete();
    }

    public function like($id, $userId)
    {
        return DB::transaction(function () use ($id, $userId) {
            $record = UserLikeClassicCase::whereUserId($userId)->whereClassicCasesId($id)->first();
            if ($record) {
                $record->delete();
                DB::table('kd_classic_cases')->where('id', $id)->decrement('like_count');
                return 0;
            }
            UserLikeClassicCase::create([
                'user_id' => $userId,
                'classic_cases_id' => $id,
            ]);
            DB::table('kd_classic_cases')->where('id', $id)->increment('like_count');
            return 1;
        });
    }

    public function share($id)
    {
        DB::table('kd_classic_cases')->where('id', $id)->increment('share_count');
    }

    public function detail($id)
    {
        DB::table('classic_case')->where('id', $id)->increment('view_count');
        $data = ClassicCase::query()->where('id', $id)->firstOrFail()->toArray();
        return $data;
    }
}
